package com.wunong.smart.commission.starter.converter;

import com.wunong.smart.commission.client.enums.CommissionStatusEnum;
import com.wunong.smart.commission.domain.model.CommissionDomain;
import com.wunong.smart.commission.starter.infrastructure.po.CommissionPO;
import com.wunong.smart.domain.platform.common.BizSource;
import com.wunong.smart.domain.platform.entify.User;
import com.wunong.smart.domain.platform.utils.TimeUtils;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author create by zealot.zt
 */
public class CommissionConverter {

    public static CommissionDomain toDomain(CommissionPO po) {
        if (po == null) {
            return null;
        }

        CommissionDomain domain = CommissionDomain.of();
        domain.init(po);

        domain.setUser(User.of(po.getUserId(), po.getUserRole()));

        String bizId = po.getBizId();
        if (Objects.nonNull(bizId)) {
            domain.setSource(BizSource.of(po.getBizId(), po.getBizType()));
        }

        // 状态
        domain.setCommissionType(po.getCommissionType());

        // 状态信息
        domain.setStatusEnum(CommissionStatusEnum.of(po.getStatus()));
        domain.setFinishTime(TimeUtils.asLocalDateTime(po.getFinishTime()));
        domain.setContent(po.getContent());

        return domain;
    }

    public static List<CommissionDomain> toDomain(List<CommissionPO> pos) {
        if (CollectionUtils.isEmpty(pos)) {
            return null;
        }

        return pos.stream().map(CommissionConverter::toDomain).collect(Collectors.toList());
    }

    public static CommissionPO toPo(CommissionDomain domain) {
        if (domain == null) {
            return null;
        }

        CommissionPO po = CommissionPO.of();
        po.init(domain);

        User user = domain.getUser();
        if (Objects.nonNull(user)) {
            po.setUserId(user.getIdLong());
            po.setUserRole(user.getRole());
        }

        BizSource source = domain.getSource();
        if (Objects.nonNull(source)) {
            po.setBizId(source.getBizId());
            po.setBizType(source.getBizType());
        }

        po.setCommissionType(domain.getCommissionType());
        po.setStatus(domain.getStatus());
        po.setContent(domain.getContent());

        return po;
    }

}
